#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'tian'
__data__ = '2018/8/29 17:11'

import requests
from config import settings
from modules.user_loging import CommonalityUserMethod

class LoginHandle:

    def __init__(self):
        self.url         = settings.URL
        self.getuser_obj = CommonalityUserMethod()


    def get_token(self):
        """获取用户登录后的token"""
        return  self.getuser_obj.get_load_usertoken()['Obj']['Token']


    def get_sendcode(self,*args,**kwargs):
        '''例子伪代码,测试用例中调用该方法传入的参数args来构造request的请求参数'''
        url = "{0}{1}".format(self.url,"/Member/SendCode")
        payload = {
            "phone":args[0],
            "type" :args[1],
        }

        return requests.post(url=url,data=payload).json()


    def login_sucess(self,*args,**kwargs):
        """伪代码"""
        url = "{0}{1}".format(self.url,"/Member/Login")
        payload = {
            "phone":args[0],
            "code" :args[2],
            "loginModel":args[1],
            "rid" : ""
        }
        rest = requests.post(url,data=payload).json()
        if rest["Code"]==0:
            self.getuser_obj.write_clear_user_token(rest)

        return rest

    def resetpwd(self,*args,**kwargs):
        """伪代码修改密码后更新密码"""
        url = "{0}{1}".format(self.url,"/Member/ReSetPwd")
        payload ={
            "phone":args[0],
             "pwd" : args[1]
        }
        rest = requests.post(url,data=payload).json()
        if rest["Code"]==0:
            self.getuser_obj.Undata_newIphone(args[1])
        return rest


    def intocentent(self,*args,**kwargs):
        '''携带token进入个人中心'''
        token = self.get_token()
        url   = "{0}{1}".format(self.url,"/Member/centent")
        header={
            "x-token":token
        }
        return requests.post(url=url,header=header)

    def logout(self,*args,**kwargs):
        '''退出登录'''
        url = "{0}{1}".format(self.url,"/Member/Logout")
        pass




